<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Invoices;
use App\Rep\EmployeeRep;
use App\Rep\NumberExtractorRep;
use App\Rep\Rbac\UserRep;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use App\Library\PrintingOptions\PrintingOptions;

class NumberExtractor extends Controller
{
    //抽号机管理
    protected $rep;

    public function __construct(Request $request)
    {
        $this->rep = new NumberExtractorRep();
    }

    /**
     * 管理卡登陆
     * 员工卡号登录
     */
    public function signInCardNumber(Request $request){
        $params = $request->all();
        $userRep = new UserRep();
        $data = $userRep->signInCardNumberRep($params);
        return re($data['code'],$data['data'],$data['msg']);
    }

    /**
     * 员工输入卡号，查询可领取订单
     */
    public function getOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $list = $this->rep->getOrderListByEmployeeCardNumnberRep($params);
        return re(0,$list,'success');
    }

    /**
     * 已完成，未完成
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCompletedOrUnfinishedOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $employeeRep = new EmployeeRep();
        $list = $employeeRep->searchCompletedOrUnfinishedOrderChildRep($params);
        return re(0,$list,'success');
    }

    /**
     * 获取订单详情
     * @param Request $request
     */
    public function getOrderChildInfoByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $list = $this->rep->getOrderChildInfoByEmployeeCardNumnberRep($params);
        return re(0,$list,'success');
    }



    /**
     * 抽号机
     * 员工领取订单
     * @param Request $request
     */
    public function drawOrderChildByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $employeeRep = new EmployeeRep();
        $data = $employeeRep->drawOrderChildRep($params);
        if($data['code'] != 0){
            return re($data['code'],$data['data'],$data['msg']);
        }
        $order_child = $data['data']->toArray();
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();

        $pritingOptions = new PrintingOptions();
        $array = $pritingOptions->getOrderInvoices($list,$order_child);

        //p($order_child['employee']['employee_name']);
//        $employee_name = \App\Models\Employee::where('user_id',$params['user_id'])->value('employee_name');
//
//        $array = [];
//        $res = [
//            'title'=>'接单单据',
//            'qr_code'=>json_encode(["order_id"=>$order_child['order_id'],"order_child_id"=>$order_child['id']]),
//            'order_number'=>$order_child['order']['order_number'],
//            'customer_name'=>$order_child['order']['customer']['customer_name'],
//            'order_date'=>$order_child['order']['order_date'],
//            'delivery_date'=>$order_child['order']['delivery_date'],
//            'serial_number'=>$order_child['order']['serial_number'],
//            'emergency_procedure'=>$order_child['order']['emergency_procedure'],
//            'model_number'=>'【'.$order_child['order']['product']['product_name'].'】'.$order_child['order']['model_number']['model_number'],
//            'specification_id'=>$order_child['order']['specification']['specification'],
//            'color'=>$order_child['order']['color']['color'],
//            'direction'=>$order_child['order']['direction'],
//            'remark'=>$order_child['order']['remark'],
//            'draw_time'=>$order_child['draw_time'],
//            'employee_user'=>$employee_name,
//            'employee_user_id'=>$params['user_id'],
//            'job_type'=>$order_child['job_type']['job_type'],
//        ];
//        foreach($list as $k=>$v){
//            $array[$k]['field'] = $v->field;
//            $array[$k]['size'] = $v->size;
//            $array[$k]['field_name'] = $v->field_name;
//            $array[$k]['field'] = $v->field;
//            $array[$k]['value'] = $res[$v->field];
//        }
        return re($data['code'],$array,$data['msg']);
    }

    /**
     * 查询外包订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getEpibolyOrderListByEmployeeCardNumnber(Request $request){
        $params = $request->all();
        $list = $this->rep->getEpibolyOrderListByEmployeeCardNumnberRep($params);
        return re(0,$list,'success');
    }

    /**
     * 获取外包详情
     */
    public function getEpibolyOrderInfo(Request $request){
        $params = $request->all();
        $order_child_list = $this->rep->getEpibolyOrderInfoRep($params);
        //p($order_child_list);
        $list = Invoices::where('is_show',1)->orderBy('sort','asc')->get();

        $pritingOptions = new PrintingOptions();
        $array = $pritingOptions->getEpibolyOrderInvoices($list,$order_child_list);

//        $array = [];
//        foreach($order_child_list as $k=>$v){
//            $res = [];
//            $res = [
//                'title'=>'外包单据',
//                'qr_code'=>json_encode(["order_id"=>$v['order_id'],"order_child_id"=>$v['id']]),
//                'order_number'=>$v['order']['order_number'],
//                'customer_name'=>$v['order']['customer']['customer_name'],
//                'order_date'=>$v['order']['order_date'],
//                'delivery_date'=>$v['order']['delivery_date'],
//                'serial_number'=>$v['order']['serial_number'],
//                'emergency_procedure'=>$v['order']['emergency_procedure'],
//                'model_number'=>'【'.$v['order']['product']['product_name'].'】'.$v['order']['model_number']['model_number']??'',
//                'specification_id'=>$v['order']['specification']['specification'],
//                'color'=>$v['order']['color']['color'],
//                'direction'=>$v['order']['direction'],
//                'remark'=>$v['order']['remark'],
//                'draw_time'=>$v['draw_time'],
//                'employee_user'=>$v['employee']['company_name']??'',
//                'employee_user_id'=>$v['employee_user_id'],
//                'job_type'=>$v['job_type']['job_type'],
//                'epiboly_time'=>$v['epiboly_time']
//            ];
//            $arrs = [];
//            foreach($list as $kk=>$vv){
//                $arr = [];
//                if($vv->field == 'draw_time'){
//                    $arr['field'] = 'epiboly_time';
//                    $arr['size'] = $vv->size;
//                    $arr['field_name'] = '外包时间';
//                    $arr['value'] = $res['epiboly_time'];
//                    $arrs[] = $arr;
//                }else{
//                    if(!in_array($vv->field,['employee_user','employee_user_id'])){
//                        $arr['field'] = $vv->field;
//                        $arr['size'] = $vv->size;
//                        $arr['field_name'] = $vv->field_name;
//                        $arr['value'] = $res[$vv->field];
//                        $arrs[] = $arr;
//                    }
//                }
//            }
//            $array[] = $arrs;
//        }

        return re(0,$array,'success');
    }




    /**
     * 生成二维码图片
     * @param $url
     * @param string $img
     * @param string $filename
     */
//    public function getQrcodeImg($url,$file_path, $filename)
//    {
//        if(!file_exists($file_path))
//            mkdir($file_path);
//        //使用QrCode类，生成二维码图片
//        //format() 修改二维码图片格式，目前仅支持PNG、SVG 和 EPS三种格式，必须在其他格式化方法前调用
//        QrCode::format('png')
//            //size() 修改二维码图片尺寸：像素
//            ->size(198)
//            //margin()二维码边距设置,默认为4
//            ->margin(2)
//            //容错级别设置 L（7%）、M（15%）、Q（25%）、H（30%），容错级别越高，字节码回复率越大，二维码里能存储的数据越少
//            //->errorCorrection("L")
//            //merge($filename, $percentage, $absolute)Logo或者头像放到二维码图片中，参数分别为 图片路径，百分比，切换绝对路径
//            //->merge($img, .3, true)
//            //指定编码
//            ->encoding('UTF-8')
//            //generate($text , $filename)$text 二维码数据 $filename 文件名及保存路径
//            ->generate($url, $file_path.$filename);
//    }


    /**
     * 抽号机单据设置
     * 获取抽号机单据设置列表
     */
    public function getInvoicesList(Request $request){
        $list = Invoices::orderBy('sort','asc')->get();
        return re(0,$list,'success');
    }

    /**
     * 抽号机单据设置
     * 设置字段是否显示  field = is_show  value=1
     * 设置字体大小      field = size     value=1
     */
    public function setInvoicesValue(Request $request){
        $id = $request->input('id');
        $field = $request->input('field');
        $value = $request->input('value');
        $model = Invoices::find($id);
        $model->$field = $value;
        $model->save();
        return re(0,$model,'success');
    }

    /**
     * 抽号机单据设置
     * 设置字段顺序
     * @param Request $request
     */
    public function setInvoicesSort(Request $request){
        $arr = $request->input('sort_arr');
        DB::beginTransaction();
        try{
            foreach($arr as $k=>$v){
                Invoices::where('id',$v['id'])->update(['sort'=>$v['sort']]);
            }
            DB::commit();
            re(0,[],'success');
        }catch(\Exception $exception){

            DB::rollBack();
            return re(2001,[],$exception->getMessage());
        }
    }




}
